package com.app.qbake.module.publish.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wind.base.BaseActivity;
import com.wind.base.bean.UploadFile;
import com.wind.base.utils.ActivityUtil;
import com.wind.base.utils.Navigator;
import com.wind.base.widget.WidthRatioFrameLayout;
import com.app.qbake.R;
import com.app.qbake.module.publish.event.VideoDelEvent;
import com.app.qbake.module.publish.event.VideoUploadedEvent;
import com.app.qbake.module.publish.layout.FileUploadLayout;
import com.app.qbake.util.MediaUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;



public class VideoPreviewActivity extends BaseActivity implements FileUploadLayout.UploadFileListener {

    private FileUploadLayout mFileUploadLayout;
    public static void start(Context context,String videoPath){
        Navigator.navigate(context,VideoPreviewActivity.class,videoPath);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_preview);

        ImageView iv_cover=$(R.id.iv_cover);
        WidthRatioFrameLayout rfl=$(R.id.rfl);
        final String videoPath=Navigator.getSerializableExtra(this);//getIntent().getStringExtra(MediaRecorderActivity.VIDEO_URI);
        //final String videoPath=Navigator.getSerializableExtra(this);
        //TODO 获取视频第一帧

        Bitmap bitmap = MediaUtil.getVideoFirstFrame(videoPath);
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        rfl.setmRatio(height/(float)width);

        iv_cover.setImageBitmap(bitmap);

        UploadFile videoFile=new UploadFile(videoPath);
        videoFile.setDontCompress(true);
        List<UploadFile> uploadFiles=new ArrayList<>();
        uploadFiles.add(videoFile);
        mFileUploadLayout=new FileUploadLayout(getActivity(),uploadFiles);
        mFileUploadLayout.setUploadFileListener(this);
        TextView tv_upload=$(R.id.tv_upload);
        if (videoPath.startsWith("http")){
            tv_upload.setText("重新上传");
        }else {
            tv_upload.setText("立即上传");
        }


        tv_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //上传视频
                if (videoPath.startsWith("http")){//重新上传
                    VideoUploadTipActivity.start(getActivity());
                }else {
                    showOpLoadingIndicator();
                    mFileUploadLayout.uploadFile();
                }
            }
        });

        $(R.id.iv_play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //VideoPlayerActivity.start(getActivity(),videoPath);
                MediaUtil.startVideoPlayer(getActivity(),videoPath);
            }
        });
    }

    @Override
    protected void setTitle() {
        mTitleBar.setTitle("视频预览");
        mTitleBar.setRightText("删除");
        mTitleBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new VideoDelEvent());
                ActivityUtil.finish(getActivity());
            }
        });
    }

    @Override
    public void onUploadError() {
        hideOpLoadingIndicator();
    }

    @Override
    public void onUploadFileProgress() {

    }

    @Override
    public void onUploadFileSuccess(List<String> urls) {
        hideOpLoadingIndicator();
        String videoUrl=urls.get(0);
        EventBus.getDefault().post(new VideoUploadedEvent(videoUrl));
        ActivityUtil.finish(getActivity());

    }
}
